package cn.cocho.android.mc.comp.listview.pdlv;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import cn.cocho.android.mc.R;
import cn.cocho.android.mc.base.Page;
import cn.cocho.android.mc.comp.listview.pdlv.PullDownListView.PullDownListViewListener;

public abstract class PullDownListViewAdapter<T> extends BaseAdapter implements PullDownListViewListener{

	private static final int  loadDataFinishFLag = 888;
	private static final int  loadingAfterFlag = 889;
	private static final int  loadMoreFlag = 890;
	private static final int  duration = 500;
	protected PullDownListView listView;
	
	public List<T> itemList;
	private Page<T> page;
	
	boolean pageEnable = false;
	boolean loadingFlag = false;
	boolean emptyViewEnable = true;
	
	View emptyView;
	View loadingView;
	
	AbsListView.LayoutParams centerLp;
	
	OnRefreshAfterListener onRefreshAfterListener;
	
	LayoutInflater inflater;
	
	public PullDownListViewAdapter(PullDownListView listView){
		this.listView = listView;
		init();
	}
	
	private void init(){
		inflater = (LayoutInflater)listView.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		page = new Page<T>();
		itemList = new ArrayList<T>();
	}
	
	@Override
	public int getCount() {
		if (itemList == null || itemList.size() == 0) {
			return 1;
		}
		return itemList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return itemList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (itemList == null || itemList.size() == 0) {
			if (loadingFlag) {
				return getLoadingView();
			} else {
				return getEmptyView();
			}
		}

		if (getLoadingView().equals(convertView) || getEmptyView().equals(convertView)) {
			convertView = null;
		}

		return getItemView(position, convertView, parent);
	}

	private View getEmptyView(){
		if(emptyView == null){
			emptyView = inflater.inflate(R.layout.pull_down_listview_empty_view, null);
		}
		if(emptyViewEnable){
			emptyView.setLayoutParams(getCenterLp());
			emptyView.setVisibility(View.VISIBLE);
		}else{
			emptyView.setVisibility(View.GONE);
		}
		
		return emptyView;
	}
	
	private View getLoadingView(){
		if(loadingView == null){
			loadingView = inflater.inflate(R.layout.pull_down_listview_loading_view, null);
		}
		loadingView.setLayoutParams(getCenterLp());
		return loadingView;
	}
	
	public void loading(){
		loadingFlag = true;
		itemList.clear();
		notifyDataSetChanged();
		loadingBefore();
		new Thread(new Runnable() {
			@Override
			public void run() {
				refresh();
				mHandler.sendEmptyMessage(loadingAfterFlag);
			}
		}).start();
	}
	
	private void updatePageData(boolean refreshOrloadMore) {
		try {
			if(page == null){
				page = new Page<T>();
			}
			
			if (refreshOrloadMore) {
				page.setPageNo(1);
				page.setRows(null);
			} else {
				page.setPageNo(page.getPageNo() + 1);
			}

			page = loadPageDatas(page);

			if (refreshOrloadMore) {
				itemList.clear();
			}

			if (page != null) {
				List<T> rows = page.getRows();
				if (rows != null) {
					itemList.addAll(rows);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (page != null) {
				mHandler.sendEmptyMessage(loadMoreFlag);
			}
		}
	}
	
	private void refresh() {
		if (pageEnable) {
			updatePageData(true);
		}else{
			itemList = loadListDatas();
		}
		mHandler.sendEmptyMessage(loadDataFinishFLag);
	}

	private void loadMore() {
		if (pageEnable) {
			updatePageData(false);
		}

		mHandler.sendEmptyMessage(loadDataFinishFLag);
	}
	
	@Override
	public void onRefresh() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				sleep(duration);
				refresh();
			}
		}).start();
	}

	@Override
	public void onLoadMore() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				sleep(duration);
				loadMore();
			}
		}).start();
	}
	
	private void sleep(long time){
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public PullDownListViewListener getPullDownListViewListener(){
		return this;
	}

	public boolean isPageEnable() {
		return pageEnable;
	}

	public void setPageEnable(boolean pageEnable) {
		this.pageEnable = pageEnable;
	}

	public AbsListView.LayoutParams getCenterLp() {
		if(centerLp == null){
			centerLp = new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, listView.getHeight());
		}
		return centerLp;
	}
	
	public void setEmptyViewEnable(boolean emptyViewEnable) {
		this.emptyViewEnable = emptyViewEnable;
	}


	public void setItemList(List<T> itemList) {
		if(itemList == null){
			return;
		}
		this.itemList = itemList;
	}

	public void setUpdateFlag(boolean loadingFlag) {
		this.loadingFlag = loadingFlag;
	}

	protected abstract View getItemView(int position, View convertView, ViewGroup parent);
	
	protected abstract Page<T> loadPageDatas(Page<T> page);
	
	protected abstract List<T> loadListDatas();
	
	protected abstract void loadingAfter();
	
	protected abstract void loadingBefore();
	
	public interface OnRefreshAfterListener{
		public void onRefreshAfter();
	}

	public void setOnRefreshAfterListener(OnRefreshAfterListener onRefreshAfterListener) {
		this.onRefreshAfterListener = onRefreshAfterListener;
	}
	
	public void setEmptyView(View emptyView) {
		this.emptyView = emptyView;
	}

	public void setLoadingView(View loadingView) {
		this.loadingView = loadingView;
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			if(msg.what == loadDataFinishFLag){
				loadingFlag = false;
				notifyDataSetChanged();
				listView.stopRefresh();
				listView.stopLoadMore();
				if(onRefreshAfterListener != null){
					onRefreshAfterListener.onRefreshAfter();
				}
			}else if(msg.what == loadingAfterFlag){
				loadingAfter();
			}else if(msg.what == loadMoreFlag){
				if((itemList == null || itemList.size() == 0)){
					listView.setPullLoadEnable(false);
				}else{
					listView.setPullLoadEnable(page.isHasNext());
				}
			}
		}
	};

}
